package com.kasa.gps.db.table;

import java.util.HashSet;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kasa.gps.db.OrderGPSDBAdapter;
import com.kasa.gps.tool.Tool;

/**
 * 车辆信息表
 * */
public class VehiclesInfoTableAdapter extends OrderGPSDBAdapter {

	//private static final String TAB = VehiclesInfoTableAdapter.class.getSimpleName();
	
	public static final String TABLE_NAME="vehiclesinfo";
	
	//id
	public static final String ID="_id";
	//车牌号码
	public static final String LICENSE_PLATES="license_plates";
	//运输计划
	public static final String TRANSPORT_PLANS="transport_plans";
	//预期完成时间
	public static final String EXPECTED_TIME="expected_time";
	//位置更新频率
	public static final String LOCATION_UPDATE_FREQUENCY="location_update_frequency";
	//位置更新间距
	public static final String LOCATION_UPDATE_DISTANCE="location_update_distance";
	//上传服务器
	public static final String UPLOAD_SERVER="upload_server";
	//数据上传频率
	public static final String DATA_UPLOAD_FREQUENCY="data_upload_frequency";
	
	private Context context = null;
	
	private OrderGPSDBHelper dbHelper = null;
	private SQLiteDatabase sqlitedb = null;
	
	//private String dbDir = "";
	
	public VehiclesInfoTableAdapter(Context context){
		this.context = context;
		//this.dbDir = dbDir;
	}
	
	private VehiclesInfoTableAdapter open()throws SQLException{
		this.dbHelper = new OrderGPSDBHelper(context);
		this.sqlitedb = this.dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		this.dbHelper.close();
		this.sqlitedb.close();
	}
	
	/**插入新的车辆信息*/
	public boolean insertVehiclesInfo(String licensePlates,String transportPlans,String expectedTime,String locationUpdateFrequency,String locationUpdateDistance,String uploadServer,String dataUploadFrequency){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		ContentValues values = new ContentValues();
		values.put(LICENSE_PLATES, licensePlates);
		values.put(TRANSPORT_PLANS, transportPlans);
		values.put(EXPECTED_TIME, expectedTime);
		values.put(LOCATION_UPDATE_FREQUENCY, locationUpdateFrequency);
		values.put(LOCATION_UPDATE_DISTANCE, locationUpdateDistance);
		values.put(UPLOAD_SERVER, uploadServer);
		values.put(DATA_UPLOAD_FREQUENCY, dataUploadFrequency);
		
		return this.sqlitedb.insert(TABLE_NAME, null, values) != -1;
	}
	
	/**更新指定车牌的车辆其他信息*/
	public boolean updateVehiclesInfo(String licensePlates,String transportPlans,String expectedTime,String locationUpdateFrequency,String locationUpdateDistance,String uploadServer,String dataUploadFrequency){
		
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		ContentValues values = new ContentValues();
		values.put(LICENSE_PLATES, licensePlates);
		values.put(TRANSPORT_PLANS, transportPlans);
		values.put(EXPECTED_TIME, expectedTime);
		values.put(LOCATION_UPDATE_FREQUENCY, locationUpdateFrequency);
		values.put(LOCATION_UPDATE_DISTANCE, locationUpdateDistance);
		values.put(UPLOAD_SERVER, uploadServer);
		values.put(DATA_UPLOAD_FREQUENCY, dataUploadFrequency);
		
		return this.sqlitedb.update(TABLE_NAME, values, LICENSE_PLATES+"='"+licensePlates+"'", null) > 0;
	}
	
	public boolean updateExpectedTime(long id,String expectedTime){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		ContentValues values = new ContentValues();
		values.put(EXPECTED_TIME, expectedTime);
		return this.sqlitedb.update(TABLE_NAME, values, ID+"="+id, null) > 0;
	}
	
	/**删除指定车牌的车辆信息*/
	public boolean deleteVehiclesInfo(String licensePlates,String transportPlans){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		return this.sqlitedb.delete(TABLE_NAME, LICENSE_PLATES+"='"+licensePlates+"' and "+ TRANSPORT_PLANS+"='"+transportPlans+"'", null) > 0;
	}
	
	/**获取指定车牌的车辆信息*/
	public Cursor getVehiclesInfo(String licensePlates,String transportPlans){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		
		return this.sqlitedb.query(true, TABLE_NAME, new String[]{LICENSE_PLATES,TRANSPORT_PLANS,EXPECTED_TIME,LOCATION_UPDATE_FREQUENCY,LOCATION_UPDATE_DISTANCE,UPLOAD_SERVER,DATA_UPLOAD_FREQUENCY}, LICENSE_PLATES+"='"+licensePlates+"' and "+ TRANSPORT_PLANS+"='"+transportPlans+"'", null, null, null, null,null);
	}
	
	public String getLocationUpdateFrequency(int id){
		return getFrequency(id,LOCATION_UPDATE_FREQUENCY);
	}
	
	public String getDataUploadFrequency(int id){
		return getFrequency(id,DATA_UPLOAD_FREQUENCY);
	}
	
	public String getLocationUpdateDistance(int id){
		return getFrequency(id,LOCATION_UPDATE_DISTANCE);
	}
	
	
	
	private String getFrequency(int id,String ColumnName){

		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		Cursor cursor = this.sqlitedb.query(TABLE_NAME, new String[]{ColumnName}, ID+"="+id, null, null, null, null);
		String frequency = "";
		if(cursor.getCount()>0){
			try {
				cursor.moveToFirst();
				frequency = cursor.getString(cursor.getColumnIndexOrThrow(ColumnName));
			} catch (Exception e) {
				frequency = "0-1";
				Log.i("getFrequency "+ColumnName, e.toString());
			}
			
		}
		cursor.close();
		return frequency;
	
	}
	
	public String getExpectedTime(int id){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		Cursor cursor = this.sqlitedb.query(TABLE_NAME, new String[]{EXPECTED_TIME}, ID+"="+id, null, null, null, null);
		String time = "";
		if(cursor.getCount()>0){
			try {
				cursor.moveToFirst();
				time = cursor.getString(cursor.getColumnIndexOrThrow(EXPECTED_TIME));
			} catch (Exception e) {
				time= "";
			}
			
		}
		cursor.close();
		return time;
	}
	
	public String getUploadServer(int id){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		Cursor cursor = this.sqlitedb.query(TABLE_NAME, new String[]{UPLOAD_SERVER}, ID+"="+id, null, null, null, null);
		String server = "";
		if(cursor.getCount()>0){
			try {
				cursor.moveToFirst();
				server = cursor.getString(cursor.getColumnIndexOrThrow(UPLOAD_SERVER));
			} catch (Exception e) {
				server= "";
			}
			
		}
		cursor.close();
		return server;
	}
	
	/**获取所有的车牌及相关运输计划*/
	public Cursor getAllLicensePlatesAndTransportPlans(){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		
		return this.sqlitedb.query(TABLE_NAME, new String[]{ID,LICENSE_PLATES,TRANSPORT_PLANS,EXPECTED_TIME}, null, null, null, null, null);
	}
	
	public Set<String> getAllUploadServer(){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		Cursor cursor = this.sqlitedb.query(TABLE_NAME, new String[]{UPLOAD_SERVER}, null, null, null, null, null);
		Set<String> set = new HashSet<String>();
		if(cursor.getCount()>0){
			int id = cursor.getColumnIndexOrThrow(UPLOAD_SERVER);
			cursor.moveToFirst();
			do{
				String str = cursor.getString(id);
				if(!str.trim().equals("")){
					String []s = str.split(Tool.SPLIT_ADDR_PORT);
					if(s.length>0){
						set.add(s[0]);
					}
				}
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return set;
	}
}
